﻿using CRM.Attributes;
using CRM.Authorization;
using CRM.Authorization.Permissions;
using CRM.Enums;
using CRM.Extensions;
using CRM.ViewModels.User;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
//using PCustomerManager = CRM.Authorization.Permissions.CustomerManager;
//using PDataMarketing = CRM.Authorization.Permissions.DataMarketing;
//using PDataSearch = CRM.Authorization.Permissions.DataSearch;
//using PEmailMarketing = CRM.Authorization.Permissions.EmailMarketing;
//using PSocialMarketingTasks = CRM.Authorization.Permissions.SocialMarketingTasks;
//using PSystemManager = CRM.Authorization.Permissions.SystemManager;
//using PUserManager = CRM.Authorization.Permissions.UserManager;

namespace CRM.Models
{
    [Table("rule_template")]
    [Comment("用户管理_权限模版")]
    [Index(nameof(Name), Name = "Name")]
    [Index(nameof(CompanyId), Name = "CompanyId")]
    [Index(nameof(OemId), Name = "OemId")]
    [Index(nameof(State), Name = "State")]
    public class RuleTemplate : BaseModel
    {
        public RuleTemplate() { }
        public RuleTemplate(Flag flag)
        {
            SetData(flag);
        }

        [Column("id"), Key, Required]
        public int Id { get; set; }

        [Column("name", TypeName = "varchar(255)"), Comment("权限模版名称"), Required]
        public string Name { get; set; }

        [Column("remark", TypeName = "varchar(255)"), Comment("备注信息"), Required]
        public string Remark { get; set; } = string.Empty;

        [Column("state", TypeName = "tinyint"), Comment("状态1启用2禁用"), Required]
        public States State { get; set; } = States.ENABLE;

        [Column("sort", TypeName = "int(11)"), Comment("排序字段"), Required]
        public long Sort { get; set; }

        [Column("company_id", TypeName = "int(11)"), Comment("公司ID"), Required]
        public int CompanyId { get; set; }

        [Column("oem_id"), Comment("贴牌OEM ID(分区id)"), Required]
        public int OemId { get; set; }


        //权限菜单模块
        [Column("SystemManager"), Comment("系统管理"), Required, Module(nameof(SystemManager))]
        public SystemManager SystemManager { get; set; } = (SystemManager)Flag.BASE;

        [Column("UserManager"), Comment("用户管理"), Required, Module(nameof(UserManager))]
        public UserManager UserManager { get; set; } = (UserManager)Flag.BASE;

        [Column("customsData"), Comment("海关数据"), Required, Module(nameof(customsData))]
        public customsData customsData { get; set; } = (customsData)Flag.BASE;

        [Column("DataSearch"), Comment("全球搜索"), Required, Module(nameof(DataSearch))]
        public DataSearch DataSearch { get; set; } = (DataSearch)Flag.BASE;



        [Column("CustomerManager"), Comment("客户管理"), Required, Module(nameof(CustomerManager))]
        public CustomerManager CustomerManager { get; set; } = (CustomerManager)Flag.BASE;

        [Column("EmailMarketing"), Comment("邮件营销"), Required, Module(nameof(EmailMarketing))]
        public EmailMarketing EmailMarketing { get; set; } = (EmailMarketing)Flag.BASE;

        [Column("SocialMarketingTasks"), Comment("社交营销"), Required, Module(nameof(SocialMarketingTasks))]
        public SocialMarketingTasks SocialMarketingTasks { get; set; } = (SocialMarketingTasks)Flag.BASE;

        [Column("BuyersData"), Comment("买家数据"), Required, Module(nameof(BuyersData))]
        public BuyersData BuyersData { get; set; } = (BuyersData)Flag.BASE;

        [Column("LoggingInfo"), Comment("日志信息"), Required, Module(nameof(LoggingInfo))]
        public LoggingInfo LoggingInfo { get; set; } = (LoggingInfo)Flag.BASE;

        [Column("DataMarketing"), Comment("数据营销"), Required, Module(nameof(DataMarketing))]
        public DataMarketing DataMarketing { get; set; } = (DataMarketing)Flag.BASE;

        internal void SetData(AuthInfo authInfo,EntityEntry<RuleTemplate> edt)
        {
            SetData(Flag.BASE, edt);
            authInfo.Data.ForEach(i => this.SetPropOrFieldValue(i.Module, this.GetPropOrFieldValue<long>(i.Module) | i.PermissionFlag));
        }
        private void SetData(Enum @enum, EntityEntry<RuleTemplate> edt = null) {
            Action<string> edtInvoke = edt.IsNull() ? (s) => s.ToString(): (string s) => edt.Property(s).IsModified = true;
            GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(ModuleAttribute), false).Any()).ToList().ForEach(p => {
                p.SetValue(this, Convert.ToInt64(@enum));
                edtInvoke(p.Name);
            });
        }
        /// <summary>
        /// 用于User创建时生成用户权限
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, long> GetData()
        {
            Dictionary<string, long> dic = new();
            foreach (var Propertie in GetType().GetProperties())
            {
                var Attribute = Propertie.GetCustomAttributes(typeof(ModuleAttribute), false).FirstOrDefault() as ModuleAttribute;
                if (Attribute.IsNotNull()) {
                    dic.Add(Attribute.Module, (long)Propertie.GetValue(this));
                }
            }
            return dic;
        }
        public IEnumerable<Claim> GetClaims(int OemID) {
            List<Claim> lst = new();
            foreach (var item in GetData())
            {
                lst.Add(new Claim(item.Key, item.Value) { OemId = OemID });
            }
            return lst;
        }
    }
}
